(function() {

    'use strict';

    angular
        .module('app')
        .controller('PreSalesController', PreSalesController);

    PreSalesController.$inject = ['$scope', '$http', '$window', '$timeout', 'CartModel', '$location', '$state', '$stateParams', 'API', 'ENUM'];

    function PreSalesController($scope, $http, $window, $timeout, CartModel, $location, $state, $stateParams, API, ENUM) {

        $scope.navTitle = $stateParams.navTitle;
        $scope.navStyle = $stateParams.navStyle;
        if (!$scope.navStyle) {
            $scope.navStyle = 'default';
        }

        $scope.rows = null;
        $scope.isEmpty = false;
        $scope.isLoaded = false;
        $scope.isLoading = false;
        $scope.cartModel = CartModel;

        $scope.touchProduct = function (part_id) {
            $state.go('pre-sales-info', {
                part_id: part_id
            });
        };

        function _reload() {
            if ($scope.isLoading)
                return;

            $scope.rows = null;
            $scope.isEmpty = false;
            $scope.isLoaded = false;

            _fetch();
        }

        function _fetch() {
            $scope.isLoading = true;
            API.goods.getPartList().then(function(data) {
                $scope.rows = data;
                $scope.isLoaded = true;
                $scope.isLoading = false;
            });
        }

        _reload();
    }

})();
